<?php

/*
 * Created on 15/05/2006
 *
 * @author Rafael Guerrero
 */
class DoctypeHandler extends BaseHandler {

	/**
	 * @type MDocparameter
	 */
	private $currDocParameter;

	// ----------------------------------------
	// Props
	// ----------------------------------------
	/**
	 * @type MDoctype
	 */
	private $docType;

	/**
	 * @type array
	 */
	private $fieldByName = array ();
	
	/**
	 * @type array
	 */
	private $variantByName = array ();

	// ----------------------------------------

	public function __construct() {
		// Empty
	}

	// ----------------------------------------
	// Handler Methods
	// ----------------------------------------

	private function docTypeBeg(& $parser, $tag, $attributes) {
		$this->docType = BaseHandler :: $session->create('MDocType');
		$this->docType->setName($attributes['name']);
	}

	private function parameterBeg(& $parser, $tag, $attributes) {

		$this->currDocParameter = & parent :: $session->create('MDocParameter');
		$this->currDocParameter->setKey($attributes['key']);

		$docParameterMap = & $this->docType->getDocParameterMap();
		$docParameterMap[] = $this->currDocParameter;
	}

	private function captionEnd(& $parser, $tag) {
		$this->docType->setCaption($this->text);
	}

	private function parameterEnd(& $parser, $tag) {
		$this->currDocParameter->setValue($this->text);
	}

	private function docFieldEnd(& $parser, $tag) {
		$docField = & parent :: $session->create('MDocfield');
		$docField->setName($this->text);

		$docFieldList = & $this->docType->getDocFieldList();
		$docFieldList[] = $docField;

		if (isset ($this->fieldByName[$docField->getName()])) {
			throw new Exception("Duplicated field " + docField . getName());
			exit ();
		}

		$this->fieldByName[$docField->getName()] = $docField;
	}
	
	private function variantEnd(& $parser, $tag) {
		$variant = & parent :: $session->create('MVariant');
		$variant->setName($this->text);

		$variantList = & $this->docType->getVariantList();
		$variantList[] = $variant;

		if (isset ($this->variantByName[$variant->getName()])) {
			throw new Exception("Duplicated variant " + variant . getName());
			exit ();
		}

		$this->variantByName[$variant->getName()] = $variant;
	}

	// ----------------------------------------
	// BaseHandler Methods
	// ----------------------------------------
	public function startElement(& $parser, $tag, $attributes) {
		parent :: startElement($parser, $tag, $attributes);

		switch ($tag) {
			case 'docType' :
				$this->docTypeBeg($parser, $tag, $attributes);
				break;
			case 'parameter' :
				$this->parameterBeg($parser, $tag, $attributes);
				break;
		}
	}

	public function endElement(& $parser, $tag) {
		parent :: endElement($parser, $tag);
		switch ($tag) {
			case 'caption' :
				$this->captionEnd($parser, $tag);
				break;
			case 'parameter' :
				$this->parameterEnd($parser, $tag);
				break;
			case 'docField' :
				$this->docFieldEnd($parser, $tag);
				break;
			case 'variant' :
				$this->variantEnd($parser, $tag);
				break;
		}
	}

	// ----------------------------------------
	// Props Methods
	// ----------------------------------------
	/**
	 * @return MDocType
	 */
	public function getDocType() {
		return $this->docType;
	}

	/**
	 * @return array
	 */
	public function getFieldByName() {
		return $this->fieldByName;
	}
	
	/**
	 * @return array
	 */
	public function getVariantByName() {
		return $this->variantByName;
	}
}
?>
